package com.example.sneakerappforme1105.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sneakerappforme1105.GoodsDetailActivity;
import com.example.sneakerappforme1105.R;
import com.example.sneakerappforme1105.bean.Address;
import com.example.sneakerappforme1105.bean.Goods;
import com.example.sneakerappforme1105.bean.Order;
import com.example.sneakerappforme1105.bean.OrderItem;

import java.util.HashMap;
import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter{
    // 查询所有订单
    private Context mContext;
    private List<Order> mList;
    private HashMap<Long,List<OrderItem>>map;   // 订单id -> 订单项
    private HashMap<Long, Goods> mGoodsMap;
    private OnButtonListener mButtonListener;
    private OnDeleteListener mDeleteListener;
    // 定义一个循环视图列表项的长按监听器接口
    public interface OnButtonListener {
        void onButtonClick(View view, int position);

    }

    public interface OnDeleteListener {
        void delete(View view, int position);
    }

    public void setButtonListener(OnButtonListener mListener){
        this.mButtonListener = mListener;
    }
    public void setDeleteListener(OnDeleteListener mDeleteListener){this.mDeleteListener = mDeleteListener;}



    public OrderAdapter(Context mContext, List<Order>mList, HashMap<Long,List<OrderItem>>map, HashMap<Long, Goods> mGoodsMap){
        this.mContext = mContext;
        this.mList = mList;
        this.map = map;
        this.mGoodsMap = mGoodsMap;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_order,viewGroup,false);
        return new ItemHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        Order item = mList.get(position);   // 能够得到订单id
        ItemHolder holder = (ItemHolder) viewHolder;
        holder.bindBean(item);
        List<OrderItem> itemList = map.get(item.getOrderId());
        LinearLayoutManager llm = new LinearLayoutManager(mContext);
        holder.rv_orderItem.setLayoutManager(llm);  // 线性布局
        // 设置适配器
        OrderItemAdapter adapter = new OrderItemAdapter(mContext,itemList,mGoodsMap);// map
        holder.rv_orderItem.setAdapter(adapter);

        holder.bt_pay.setOnClickListener(view -> {
            if(mButtonListener!=null){
                Log.d("OrderAdapter", "按钮点击，位置: " + position);
                mButtonListener.onButtonClick(view,holder.getAdapterPosition());
            }
        });


        holder.bt_action.setOnClickListener(view -> {
            Log.d("OrderAdapter", "按钮点击，位置: " + position);
            mDeleteListener.delete(view,holder.getAdapterPosition());
        });
//        holder.ll_order.setOnLongClickListener(view -> {
//            if(mDeleteListener!=null){
//                Log.d("OrderAdapter", "按钮点击，位置: " + position);
//                mButtonListener.onButtonClick(view,holder.getAdapterPosition());
//            }
//            return true;
//        });
    }

    private class ItemHolder extends RecyclerView.ViewHolder {
        private TextView tv_orderTime;
        private RecyclerView rv_orderItem;
        private TextView tv_status;
        private TextView tv_cost;
        private Button bt_pay;
        private Button bt_action;

        public ItemHolder(View itemView) {
            super(itemView);
            tv_orderTime = itemView.findViewById(R.id.tv_orderTime);
            rv_orderItem = itemView.findViewById(R.id.rv_orderItem);
            tv_status = itemView.findViewById(R.id.tv_status);
            tv_cost = itemView.findViewById(R.id.tv_cost);
            bt_pay = itemView.findViewById(R.id.bt_pay);
//            ll_order = itemView.findViewById(R.id.ll_order);
            bt_action = itemView.findViewById(R.id.bt_action);
        }

        void bindBean(Order item){

            tv_orderTime.setText("订单号:"+item.getCreatedAt());
            if(item.getStatus().equals("pay")){
                tv_cost.setText("待付款￥"+String.valueOf(GoodsDetailActivity.formatDouble(item.getTotalAmount())));
//                tv_status.setText("待付款");
                tv_status.setVisibility(View.GONE);
                bt_pay.setVisibility(View.VISIBLE); // 待付款显示付款
                bt_action.setVisibility(View.VISIBLE);
            } else {
                tv_cost.setText("实付款￥"+String.valueOf(GoodsDetailActivity.formatDouble(item.getTotalAmount())));
                if(item.getStatus().equals("paid")){
                    tv_status.setText("已付款");
                    bt_action.setVisibility(View.VISIBLE);
                }else{
                    tv_status.setText("已收货");
                    bt_action.setVisibility(View.VISIBLE);
                }
            }


        }
    }



    @Override
    public int getItemCount() {
        return mList.size();
    }
}
